package mdt.figure;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.tigris.gef.base.Selection;
import org.tigris.gef.presentation.Fig;
import org.tigris.gef.presentation.Handle;
import mdt.model.ITextEdge;

import java.awt.*;

/**
 * Created by lambert on 2016/6/20.
 */
public class FigArrowTextEdge extends FigTextEdge{

	private static final long serialVersionUID = 1L;

	private static Log log = LogFactory.getLog(FigArrowTextEdge.class);

	public FigArrowTextEdge(ITextEdge owner) {
		super(owner);
	}

	protected Fig makeEdgeFig() {
		FigArrowText fig = new FigArrowText(null);
		fig.setLineWidth(0);
		return fig;
	}

	@Override
	public Selection makeSelection() {
		return new SelectionShapedEdgeResize(this) {

			int fx;
			int fy;
			int fw;
			int fh;
			static final int LOCAL_HANDLE_BASE = 10;

			public void paint(Graphics g) {
				super.paint(g);
				//g.setColor(Globals.getPrefs().handleColorFor(FigArrowTextEdge.this));
				updateInnerFigHandle();
				g.setColor(Color.red);
				Graphics2D g2d = (Graphics2D)g;

				g.fillRect(fx - HAND_SIZE / 2, fy - HAND_SIZE / 2, HAND_SIZE,
						HAND_SIZE);
				g.fillRect(fx + fw - HAND_SIZE / 2, fy - HAND_SIZE / 2, HAND_SIZE,
						HAND_SIZE);
				g.fillRect(fx - HAND_SIZE / 2, fy + fh - HAND_SIZE / 2, HAND_SIZE,
						HAND_SIZE);
				g.fillRect(fx + fw - HAND_SIZE / 2, fy + fh - HAND_SIZE / 2,
						HAND_SIZE, HAND_SIZE);

				/*
				g.setColor(Color.red);
				g2d.fillArc(cx - HANDLE_DIAMETER / 2, cy - HANDLE_DIAMETER / 2, HANDLE_DIAMETER, HANDLE_DIAMETER, 0, 360);
				g2d.fillArc(cx - HANDLE_DIAMETER / 2, cy + ch - HANDLE_DIAMETER / 2, HANDLE_DIAMETER, HANDLE_DIAMETER, 0, 360);
				g2d.fillArc(cx + cw - HANDLE_DIAMETER / 2, cy - HANDLE_DIAMETER / 2, HANDLE_DIAMETER, HANDLE_DIAMETER, 0, 360);
				g2d.fillArc(cx + cw - HANDLE_DIAMETER / 2, cy + ch - HANDLE_DIAMETER / 2, HANDLE_DIAMETER, HANDLE_DIAMETER, 0, 360);
				*/
			}

			public void hitHandle(Rectangle r, Handle h) {
				super.hitHandle(r, h);
				if (h.index == HANDLE_DRAG_ONLY) {
					updateInnerFigHandle();
					Rectangle testRect = new Rectangle(0, 0, 0, 0);
					testRect.setBounds(fx - HAND_SIZE / 2, fy - HAND_SIZE / 2,
							HAND_SIZE, fh + HAND_SIZE / 2);
					boolean leftEdge = r.intersects(testRect);
					testRect.setBounds(fx + fw - HAND_SIZE / 2, fy - HAND_SIZE / 2,
							HAND_SIZE, fh + HAND_SIZE / 2);
					boolean rightEdge = r.intersects(testRect);
					testRect.setBounds(fx - HAND_SIZE / 2, fy - HAND_SIZE / 2, fw
							+ HAND_SIZE / 2, HAND_SIZE);
					boolean topEdge = r.intersects(testRect);
					testRect.setBounds(fx - HAND_SIZE / 2, fy + fh - HAND_SIZE / 2, fw
							+ HAND_SIZE / 2, HAND_SIZE);
					boolean bottomEdge = r.intersects(testRect);
					// needs-more-work: midpoints for side handles
					if (leftEdge && topEdge) {
						h.index = LOCAL_HANDLE_BASE + Handle.NORTHWEST;
						//h.instructions = "Resize top left";
					} else if (rightEdge && topEdge) {
						h.index = LOCAL_HANDLE_BASE + Handle.NORTHEAST;
						//h.instructions = "Resize top right";
					} else if (leftEdge && bottomEdge) {
						h.index = LOCAL_HANDLE_BASE + Handle.SOUTHWEST;
						//h.instructions = "Resize bottom left";
					} else if (rightEdge && bottomEdge) {
						h.index = LOCAL_HANDLE_BASE + Handle.SOUTHEAST;
						//h.instructions = "Resize bottom right";
					}
				}
			}

			public void dragHandle(int mX, int mY, int anX, int anY, Handle hand) {
				if (hand.index < LOCAL_HANDLE_BASE) {
					super.dragHandle(mX, mY, anX, anY, hand);
					return;
				}

				hand.index -= LOCAL_HANDLE_BASE;

				final FigArrowText figArrowText = (FigArrowText)FigArrowTextEdge.this.getShapeNode();
				final Fig figText = figArrowText.getFigText();

				int arrowWidth = figArrowText.getArrowWidth();
				int arrowHeight = figArrowText.getArrowHeight();

				updateInnerFigHandle();

				final int x = fx;
				final int y = fy;
				final int w = fw;
				final int h = fh;
				int newX = x, newY = y, newWidth = w, newHeight = h;
				Dimension minSize = figText.getMinimumSize();
				int minWidth = minSize.width, minHeight = minSize.height;
				switch (hand.index) {
					case Handle.NORTHWEST:
						newWidth = x + w - mX;
						newWidth = (newWidth < minWidth) ? minWidth : newWidth;
						newHeight = y + h - mY;
						newHeight = (newHeight < minHeight) ? minHeight : newHeight;
						newX = x + w - newWidth;
						newY = y + h - newHeight;
						//fig.setHandleBox(newX, newY, newWidth, newHeight);
						if ((newX + newWidth) != (x + w)) {
							newX += (newX + newWidth) - (x + w);
						}
						if ((newY + newHeight) != (y + h)) {
							newY += (newY + newHeight) - (y + h);
						}
						int dx = newX - x;
						if (arrowWidth + dx < FigArrowText.MIN_ARROW_WIDTH) {
							dx = 0;
						} else {
							figArrowText.setArrowWidth(arrowWidth + dx);
						}
						int dy = newY - y;
						if (figText.getHeight() - dy < figText.getMinimumSize().getHeight()) {
							dy = 0;
						} else {
							figText.setHeight(figText.getHeight() - dy);
						}

						//figText.setHandleBox(newX, newY, newWidth, newHeight);
						break;
					case Handle.NORTHEAST:
						newWidth = mX - x;
						newWidth = (newWidth < minWidth) ? minWidth : newWidth;
						newHeight = y + h - mY;
						newHeight = (newHeight < minHeight) ? minHeight : newHeight;
						newY = y + h - newHeight;
						//fig.setHandleBox(newX, newY, newWidth, newHeight);
						if ((newY + newHeight) != (y + h)) {
							newY += (newY + newHeight) - (y + h);
						}

						dx = newX + newWidth - x - w;
						if (arrowWidth - dx < FigArrowText.MIN_ARROW_WIDTH) {
							dx = 0;
						} else {
							figArrowText.setArrowWidth(arrowWidth - dx);
						}
						dy = newY - y;
						if (figText.getHeight() - dy < figText.getMinimumSize().getHeight()) {
							dy = 0;
						} else {
							figText.setHeight(figText.getHeight() - dy);
						}
						break;
					case Handle.SOUTHWEST:
						newWidth = x + w - mX;
						newWidth = (newWidth < minWidth) ? minWidth : newWidth;
						newHeight = mY - y;
						newHeight = (newHeight < minHeight) ? minHeight : newHeight;
						newX = x + w - newWidth;
						//fig.setHandleBox(newX, newY, newWidth, newHeight);
						if ((newX + newWidth) != (x + w)) {
							newX += (newX + newWidth) - (x + w);
						}

						dx = newX - x;
						if (arrowWidth + dx < FigArrowText.MIN_ARROW_WIDTH) {
							dx = 0;
						} else {
							figArrowText.setArrowWidth(arrowWidth + dx);
						}
						dy = newY + newHeight - y - h;
						if (figText.getHeight() + dy < figText.getMinimumSize().getHeight()) {
							dy = 0;
						} else {
							figText.setHeight(figText.getHeight() + dy);
						}
						break;
					case Handle.SOUTHEAST:
						newWidth = mX - x;
						newWidth = (newWidth < minWidth) ? minWidth : newWidth;
						newHeight = mY - y;
						newHeight = (newHeight < minHeight) ? minHeight : newHeight;

						dx = newX + newWidth - x - w;
						if (arrowWidth - dx < FigArrowText.MIN_ARROW_WIDTH) {
							dx = 0;
						} else {
							figArrowText.setArrowWidth(arrowWidth - dx);
						}
						dy = newY + newHeight - y - h;
						if (figText.getHeight() + dy < figText.getMinimumSize().getHeight()) {
							dy = 0;
						} else {
							figText.setHeight(figText.getHeight() + dy);
						}
						break;
				}
				figArrowText.updateBounds();
				hand.index += LOCAL_HANDLE_BASE;
			}

			protected void updateInnerFigHandle() {
				Rectangle rect = ((FigArrowText)FigArrowTextEdge.this.getShapeNode()).getFigText().getBounds();
				fx = rect.x;
				fy = rect.y;
				fw = rect.width;
				fh = rect.height;
			}
		};
	}

	public boolean isSelfEdgeEnabled() {
		return false;
	}

}
